package fit.deni.hibernate;

import java.util.Collection;

import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import fit.deni.beans.Glumac;
import fit.deni.hibernate.interfaces.GlumacService;

public class GlumacServiceImpl implements GlumacService {

	private Session session;

	public GlumacServiceImpl(Session session) {
		this.session = session;
	}

	@CommitAfter
	public void addActor(Glumac glumac) {
		session.merge(glumac);
	}

	public Collection<Glumac> getAll() {
		return session.createCriteria(Glumac.class).add(Restrictions.eq("deleted", false)).list();
	}

	@CommitAfter
	public void deleteActor(Glumac glumac) {
		glumac.setDeleted(true);
		session.merge(glumac);
//		session.delete(glumac);
//		session.flush();
	}

	public Glumac getActorById(int id) {
		return (Glumac) session.createCriteria(Glumac.class).add(Restrictions.eq("id", id)).list().get(0);
	}

	public Glumac getActorByName(String name) {
		return (Glumac) session.createCriteria(Glumac.class).add(Restrictions.eq("ime", name)).list().get(0);
	}

}
